package com.graduation.mathematics.ui.fragment.student;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.graduation.mathematics.R;
import com.graduation.mathematics.adapter.BaseExpandableAdapter;
import com.graduation.mathematics.adapter.FunnyExamAdapter;
import com.graduation.mathematics.adapter.QuickItemDecoration;
import com.graduation.mathematics.base.BaseFragment;
import com.graduation.mathematics.bean.FunnyExam;
import com.graduation.mathematics.ui.activity.FunnyExamDetailActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ProjectName: mathematicsWorkbook
 * @Package: com.graduation.mathematics.ui.fragment.student
 * @ClassName: ProblemSetFragment
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/23 15:57
 * @Version: 1.0
 */
public class ProblemSetFragment extends BaseFragment {


    @BindView(R.id.recycler_funntexam)
    RecyclerView recyclerView;

    private FunnyExamAdapter funnyExamAdapter;
    List<BaseExpandableAdapter.Unit<String, FunnyExam>> dirs = new ArrayList<>();
    protected List<FunnyExam> mFunnyExamList = new ArrayList<>(); // 所有类型故障集合


    public String[] groupStrings;
    public String[][] childStrings;

    public static ProblemSetFragment newInstance(){
        return new ProblemSetFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_problem_sets;
    }

    @Override
    protected void initEventAndData() {

        initView();

        initData();
    }

    private void initView(){
        Context context = getActivity();
        if (context == null) {
            return;
        }

        LinearLayoutManager manager  = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new QuickItemDecoration(getActivity(), QuickItemDecoration.VERTICAL_LIST));

        funnyExamAdapter = new FunnyExamAdapter(context, R.layout.layout_expandalbe_group_item, R.layout.layout_expandalbe_child_item, dirs);
        recyclerView.setAdapter(funnyExamAdapter);
        funnyExamAdapter.setOnItemViewClickLitener(new FunnyExamAdapter.OnItemViewClickLitener() {
            @Override
            public void onAllViewClick(View view, int position) {
                ToastUtils.showLong("download all--" + dirs.get(position).group);
            }

            @Override
            public void onErrorViewClick(View view, int position) {
                ToastUtils.showLong("download error--" + dirs.get(position).group);
            }
        });

        funnyExamAdapter.setOnItemClickListener(new BaseExpandableAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String parent, FunnyExam funnyExam) {
                Object o = funnyExamAdapter.getItem(position);
                ToastUtils.showLong("点击了:" + parent + "-" + funnyExam.getQuestionId());

                Bundle bundle = new Bundle();
                bundle.putSerializable(FunnyExamDetailActivity.KEY, funnyExam);
                Intent intent = new Intent();
                intent.putExtra(FunnyExamDetailActivity.BUNDLE_KEY, bundle);
                intent.setClass(getActivity(), FunnyExamDetailActivity.class);
                startActivity(intent);
//                System.out.println(o);
            }
        });
    }

    private void initData(){
        groupStrings = getActivity().getResources().getStringArray(R.array.funnyexamList);
        mFunnyExamList.clear();
        dirs.clear();
        mFunnyExamList = LitePal.findAll(FunnyExam.class);
        for (int i = 0;i<groupStrings.length; i ++) {
            BaseExpandableAdapter.Unit<String, FunnyExam> unit = new BaseExpandableAdapter.Unit<>(groupStrings[i], mFunnyExamList);
            dirs.add(unit);
        }

        funnyExamAdapter.setItemList(dirs);
    }
}
